﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace IMIForUnity
{

    public enum GestureEvent
    {
        Complete,
        Cancel,
        Checking 
    }

    public abstract class ImiGesture : IGesture
    {

        public enum State
        {
            UNCHECKED,
            CHECKING,
            FINISHED,
            CANCELED
        }

        /// <summary>
        ///进度检测开始时间. 
        /// </summary>
        public double gestureCheckTime;

        /// <summary>
        ///姿势完成进度： 0 ~ 1. 
        /// </summary>
        public double gestureProgress;

        public string name;

        public State state;

        public double startTime;

        public delegate void OnGestureEvent(ImiGesture gesture, ImiPlayerInfo player, GestureEvent gevent);

        public OnGestureEvent GestureEventListeners;

        public virtual void GestureEvent(ImiGesture gesture, ImiPlayerInfo player,GestureEvent gevent) { }

        public abstract State CheckGesture(ImiPlayerInfo playerinfo, double timestamp);


        public void CheckGestureComplete(double timeStamp, float timeDetection)
        {
            float timeLeft = (float)(timeStamp - gestureCheckTime);
            float progress = timeLeft / timeDetection;
            gestureProgress = timeDetection > 0f ? (progress > 1.0f ? 1.0f : progress) : 1.0f;

            if (timeLeft >= timeDetection)
            {
                gestureCheckTime = timeStamp;
                state = State.FINISHED;
            }
        }

        public void Reset()
        {
            state = State.UNCHECKED;
            startTime =  0;
        }

        //方便选择骨骼，设定了以下常量
        public const int leftHandIndex = (int)ImiSkeleton.Index.HAND_LEFT;
        public const int rightHandIndex = (int)ImiSkeleton.Index.HAND_RIGHT;

        public const int leftWristIndex = (int)ImiSkeleton.Index.WRIST_LEFT;
        public const int rightWristIndex = (int)ImiSkeleton.Index.WRIST_RIGHT;

        public const int leftElbowIndex = (int)ImiSkeleton.Index.ELBOW_LEFT;
        public const int rightElbowIndex = (int)ImiSkeleton.Index.ELBOW_RIGHT;

        public const int leftShoulderIndex = (int)ImiSkeleton.Index.SHOULDER_LEFT;
        public const int rightShoulderIndex = (int)ImiSkeleton.Index.SHOULDER_RIGHT;

        public const int headIndex = (int)ImiSkeleton.Index.HEAD;
        public const int hipCenterIndex = (int)ImiSkeleton.Index.HIP_CENTER;
        public const int shoulderCenterIndex = (int)ImiSkeleton.Index.SHOULDER_CENTER;
        public const int spineIndex = (int)ImiSkeleton.Index.SPINE;

        public const int leftHipIndex = (int)ImiSkeleton.Index.HIP_LEFT;
        public const int rightHipIndex = (int)ImiSkeleton.Index.HIP_RIGHT;

        public const int leftFootIndex = (int)ImiSkeleton.Index.FOOT_LEFT;
        public const int rightFootIndex = (int)ImiSkeleton.Index.FOOT_RIGHT;

        public const int leftAnkleIndex = (int)ImiSkeleton.Index.ANKLE_LEFT;
        public const int rightAnkleIndex = (int)ImiSkeleton.Index.ANKLE_RIGHT;

        public const int leftKneeIndex = (int)ImiSkeleton.Index.KNEE_LEFT;
        public const int rightKneeIndex = (int)ImiSkeleton.Index.KNEE_RIGHT;

        public const int leftForearmIndex = (int)Limb.Left_Forearm;
        public const int leftPostbrachiumIndex = (int)Limb.Left_Postbrachium;
        public const int rightForearmIndex = (int)Limb.Right_Forearm;
        public const int rightPostbrachiumIndex = (int)Limb.Right_Postbrachium;
        public const int leftCalfIndex = (int)Limb.Left_Calf;
        public const int leftThighIndex = (int)Limb.Left_Thigh;
        public const int rightCalfIndex = (int)Limb.Right_Calf;
        public const int rightThighIndex = (int)Limb.Right_Thigh;
        public const int spineAngularIndex = (int)Limb.Spine;


    }
}
